package com.suishoupai.xmb.task;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.suishoupai.xmb.dao.CutEmailSecurityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Created by caojian on 2017/4/17.
 */
@Component
public class EmailTask {
    private static final Log LOG = LogFactory.getLog(EmailTask.class);
    @Autowired
    private CutEmailSecurityMapper cutEmailSecurityMapper;
    @Value("${email.once-limit}")
    private int emailOnceLimit;
    @Value("${email.once-sleep}")
    private int emailOnceSleepTime;

    //每天凌晨两点执行
    @Scheduled(cron = "0 0 2 * * ?")
    public void reset() {
        LOG.info("开始重置邮件发送数");
        int dealNum = 0;
        while ((dealNum = cutEmailSecurityMapper.reset(emailOnceLimit)) == emailOnceLimit) {
            LOG.info("已处理" + dealNum);
            try {
                Thread.sleep(emailOnceSleepTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        LOG.info("已处理" + dealNum);
        LOG.info("已处理完重置邮件发送数");
    }
}
